// pages/myself/feedback-detail/feedbackDetail.js
const app = getApp()
Page({

  /**
   * 页面的初始数据
   */
  data: {
    isAdmin: false,
    isNodate: false,
    loginUserId: '',
    CDNPATH: app.globalData.CDNPATH,
    detailFold: true,
    feedbackId: '',
    detail: {},
    replyList: [],
    replyShow: false,
    replyHeight: '',
    replyMsg: '',
    replyInfo: {},
    prePlaceholder: ''
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    const { userInfo } = app.globalData
    console.log(userInfo,'userInfo')
    this.setData({
      feedbackId: options.id,
      isAdmin: userInfo.roleCode === 'admin-exclusive' || userInfo.roleCode === 'admin-nonExclusive' || userInfo.roleCode === 'admin-promoter' || userInfo.roleCode === 'admin-admin',
      loginUserId: userInfo.userId
    })
    this.getDetail()
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  },
  toggleDetail () {
    const { detailFold } = this.data
    this.setData({
      detailFold: !detailFold
    })
    this.calcReHeight()
  },
  calcReHeight () {
    const queryHeight = this.createSelectorQuery();
    queryHeight.select('.detail').boundingClientRect();
    queryHeight.exec((res) => {
      const replyHeight = `calc(100% - ${res[0].height + 57}px)`
      this.setData({
        replyHeight
      })
    })
  },
  getDetail () {
    const { feedbackId } = this.data
    app.api.post({
      url: 'pc_feedback_detail',
      data: { id: feedbackId } // score大于0表示通过
    }).then(res => {
      this.setData({
        replyList: res.replyList,
        detail: {
          description: res.description,
          imgUrlList: res.imgUrlList,
          replyTotal: res.replyTotal,
          creator: res.creator,
          creatorName: res.creatorName,
          created: res.created
        },
        isNodate: !(Number(res.replyTotal))
      })
      const queryHeight = this.createSelectorQuery();
      queryHeight.select('.detail-all').boundingClientRect();
      queryHeight.exec((res) => {
        console.log(res[0])
        const detailOverflow = res[0].height > 175
        this.setData({
          detailOverflow
        })
        this.calcReHeight()
      })
    })
  },
  delReply (e) {
    const { id } = e.currentTarget.dataset.item
    wx.showModal({
      title: '删除后消息将不可见，确定要删除吗？',
      cancelColor: '#666666',
      confirmColor: '#006DE7',
      success: (res) => {
        if (res.confirm) {
          app.api.post({
            url: 'pc_feedback_delete',
            data: {
              id
            }
          }).then(res => {
            wx.showToast({
              title: '删除成功',
              icon: 'success',
              duration: 2000
            })
            this.getDetail()
          })
        }
      }
    })
  },
  showReply (e) {
    const { item } = e.currentTarget.dataset
    let repliedReplyId = 0
    let repliedUserId = this.data.detail.creator
    let replyUserName = ''
    if (item) {
      repliedReplyId = item.id
      repliedUserId = item.replyUserId
      replyUserName = `@${item.replyUserName}：`
    }
    this.setData({
      replyShow: true,
      replyMsg: '',
      replyInfo: {
        repliedReplyId,
        repliedUserId
      },
      prePlaceholder: replyUserName
    })
  },
  cancelReply () {
    this.setData({
      replyShow: false
    })
  },
  reply (e) {
    const { feedbackId, replyMsg: messageContent, replyInfo } = this.data
    const { repliedReplyId, repliedUserId } = replyInfo
    console.log(messageContent)
    app.api.post({
      url: 'pc_feedback_addFeedbackReply',
      data: { feedbackId, messageContent, repliedReplyId, repliedUserId }
    }).then(res => {
      this.getDetail()
      this.setData({
        replyShow: false
      })
    })
  }
})